package com.xue.utils;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.xue.constants.Constant;
import com.xue.entity.CaseInfo;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.*;
import java.util.List;

/**
 * @author xue
 * @ClassName: ExcelUtils
 * @Date: Created in 1:22 2021/10/29
 * @Description:
 */
public class ExcelUtils {

    public static List<CaseInfo> readAllData(String filePath, int sheetNum){
        File file = new File(filePath);
        ImportParams importParams = new ImportParams();
        importParams.setStartSheetIndex(sheetNum-1);
        return ExcelImportUtil.importExcel(file, CaseInfo.class, importParams);
    }

    public static CaseInfo readOneData(String filePath, int sheetNum, int startRow){
        File file = new File(filePath);
        ImportParams importParams = new ImportParams();
        importParams.setStartSheetIndex(sheetNum-1);
        importParams.setStartRows(startRow-1);
        importParams.setReadRows(1);
        return (CaseInfo) ExcelImportUtil.importExcel(file, CaseInfo.class, importParams).get(0);

    }

    public static List<CaseInfo> readSpecialData(String filePath, int sheetNum, int startRow){
        File file = new File(filePath);
        ImportParams importParams = new ImportParams();
        importParams.setStartSheetIndex(sheetNum-1);
        importParams.setStartRows(startRow-1);
        return ExcelImportUtil.importExcel(file, CaseInfo.class, importParams);

    }

    public static List<CaseInfo> readSpecialData(String filePath, int sheetNum, int startRow, int readRows){
        File file = new File(filePath);
        ImportParams importParams = new ImportParams();
        importParams.setStartSheetIndex(sheetNum-1);
        importParams.setStartRows(startRow-1);
        importParams.setReadRows(readRows);
        return ExcelImportUtil.importExcel(file, CaseInfo.class, importParams);

    }

    public static void writerListToExcel(List<CaseInfo> list, String excelTitle, String excelSheetName, String outputFilePath){
        Workbook workbook = null;
        FileOutputStream fos = null;
        try {
            ExportParams exportParams = new ExportParams(excelTitle, excelSheetName);
            workbook = ExcelExportUtil.exportExcel(exportParams, CaseInfo.class, list);
            fos = new FileOutputStream(outputFilePath);
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fos != null;
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {

//        List<CaseInfo> caseInfos = ExcelUtils.readAllData(Constant.DATA_PATH, 1);
//        ExportParams exportParams = new ExportParams("接口用例的全部数据", "login模块");
//        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CaseInfo.class, caseInfos);
//        FileOutputStream fos = new FileOutputStream("D:/test.xlsx");
//        workbook.write(fos);
//        fos.close();

        List<CaseInfo> caseInfos = readSpecialData(Constant.DATA_PATH, 2, 7, 1);
        System.out.println("caseInfos.size() = " + caseInfos.size());
        for (CaseInfo caseInfo : caseInfos) {
            System.out.println(caseInfo);
        }
    }
}
